package com.ayf.payment.game.api.wx;


import com.alibaba.fastjson.JSON;
import com.ayf.payment.game.api.dto.platform.WxMsgConfigDTO;
import com.ayf.payment.game.api.entity.platform.WxConfig;
import com.ayf.payment.game.api.entity.platform.WxMsgConfig;
import com.ayf.payment.game.api.entity.platform.WxUser;
import com.ayf.payment.game.api.service.platform.WxConfigService;
import com.ayf.payment.game.api.service.platform.WxMsgConfigService;
import com.ayf.payment.game.api.service.platform.WxUserService;
import com.ayf.payment.game.api.type.CommonConstants;
import com.ayf.payment.game.api.utils.AESUtil;
import com.ayf.payment.game.api.utils.CommonResult;
import com.ayf.payment.game.api.utils.DateUtils;
import com.ayf.payment.game.api.utils.JsonUtils;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/vx/wxmsg")
public class WdZSWXController {

    @Autowired
    private MyConstant myConstant;
    @Autowired
    private WxConfigService wxConfigService;
    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private WxMsgConfigService wxMsgConfigService;

    @PostMapping("sendMsg")
    public CommonResult sendMsg(HttpServletRequest request) throws Exception {
        String singStr = request.getHeader("EncryptedInformation");
        String dataStr = AESUtil.aes128Decrypt(singStr, WxGatewayConfig.SECRET_KEY);
        LogPortal.info("WD WXMSG WXTuisong 接收到的开区信息{}",dataStr);
        WdWxMsg wdWxMsg= JsonUtils.fromJson(dataStr,WdWxMsg.class);
        WxUser wxUser = wxUserService.getWxUserByLoginAccount(wdWxMsg.getLoginAccount());
        if (wxUser == null) {
            return  new CommonResult<>(CommonConstants.RETURN_ERROR, "商户未绑定平台微信公众号");
        }
        WxConfig config = wxConfigService.getConfig();
        if (config.getStatus() == 0) {
            return new CommonResult<>(CommonConstants.RETURN_ERROR, "平台微信公众号为关闭状态");
        }
//        String tempId = wxMsgConfigService.getTempId(WxMsgType.TIME_PARTITION_FAIL.getTemplateIdShort());
        // 根据模板code获取模板信息
        WxMsgConfig wxMsgConfig = wxMsgConfigService.getTempIdNew(WxMsgType.TIME_PARTITION_FAIL.getTemplateIdShort());
        if (wxMsgConfig == null){
            return new CommonResult<>(CommonConstants.RETURN_ERROR, "微信公众号模板未进行配置,templateId为空");
        }
        WxMsg wxMsg = new WxMsg();
        wxMsg.setOpenid(wxUser.getOpenid());

        if(wdWxMsg.getType().equals("OpenFail")|| wdWxMsg.getType().equals("MergeFail")|| wdWxMsg.getType().equals("ListUploadFailed")){
            wxMsg.setTemplateId(wxMsgConfig.getTemplateId());
            wxMsg.setFirst(wdWxMsg.getMsg());
            wxMsg.setKeyword1(wdWxMsg.getPartitionName());
            wxMsg.setKeyword2(wdWxMsg.getIp());
            wxMsg.setKeyword3(wdWxMsg.getDirectory());
            wxMsg.setKeyword4(DateUtils.dateToString(new Date()));
            wxMsg.setKeyword5("");

            wxMsg.setRemark(wdWxMsg.getMsg()+"请检查");
        }else{
//            tempId=wxMsgConfigService.getTempId(WxMsgType.TIME_PARTITION_SUCCESS.getTemplateIdShort());
            // 根据模板code获取模板信息
            wxMsgConfig = wxMsgConfigService.getTempIdNew(WxMsgType.TIME_PARTITION_SUCCESS.getTemplateIdShort());
            if (wxMsgConfig == null){
                return new CommonResult<>(CommonConstants.RETURN_ERROR, "微信公众号模板未进行配置,templateId为空");
            }
            wxMsg.setTemplateId(wxMsgConfig.getTemplateId());
            wxMsg.setFirst(wdWxMsg.getMsg());
            wxMsg.setKeyword1(wdWxMsg.getIp());
            wxMsg.setKeyword2(wdWxMsg.getPartitionName());
            wxMsg.setKeyword3("");
            wxMsg.setKeyword4("");
            wxMsg.setKeyword5("");
            wxMsg.setRemark(wdWxMsg.getPartitionName()+"-"+wdWxMsg.getDirectory()+"-"+wdWxMsg.getMsg()+"\n\r"+DateUtils.dateToString(new Date()));
        }
        // 设置key
        wxMsg.setConfig(JSON.parseObject(wxMsgConfig.getConfig()));

//       return myConstant.sendWdZhuShouMsg(config, wxMsg);
       return myConstant.sendWdZhuShouMsgNew(config, wxMsg);
    }
}
